package com.tjetc.dao;


import com.tjetc.domain.dto.OrderDTO;
import com.tjetc.domain.po.Orders;
import com.tjetc.domain.vo.BackVO;
import com.tjetc.domain.vo.OrderVo;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 订单管理Mapper接口
 *
 * @author ruoyi
 * @date 2024-07-09
 */
public interface OrdersMapper {


    /**
     * 新增订单管理
     *
     * @param orders 订单管理
     * @return 结果
     */
    public int insertOrders(OrderDTO orderDTO);

    @Select("select * from orders where  order_no=#{orderNo}")
        //type 是 const
    Orders selectByOrderNo(String orderNo);

    void updateById(Orders orders);

     @Select("SELECT * FROM orders WHERE TIMESTAMPDIFF(minute, create_time,now() ) >= #{time} and `status` = #{status}   ") //有聚合函数，建索引会失效，除非改代码，不太好改，要先查到每个订单的创建时间，改的话得不偿失
    List<Orders> findUnpaidOrdersOver30Minutes(Integer time, Integer status);


    void cancelOrder(OrderVo orderVo);

    void backMoney(OrderVo orderVo);

    @Select("select DISTINCT v.start_time  from  orders  o ,tikcet t ,pass p ,voclcontent v WHERE o.id = t.order_id   and t.pass_id = p.id  and  p.vocalconten_id = v.id  and o.order_no =#{orderNo}") //查订单表type是ref表示走了非主键或者非唯一索引，查询演唱会type是index走了覆盖，查询票也是index走覆盖，查询通道是type是eq_ref表示用了主键索引或非空唯一索引  ，其它的联查出来聚合函数的话索引失效以外都差不多
    BackVO findStartTime(String orderNo);

    List<OrderVo> list(Integer userId, String status, String startTime, String endTime);

    @Select("select * from orders where id=#{id}")
    Orders findById(Integer id);


    void finishOrder(Orders order);


    OrderVo findOrderVOByOrder(Orders orders);

    @Select("select  o.*  ,v.start_time    from  orders  o ,tikcet t ,pass p ,voclcontent v ,district d WHERE o.id = t.order_id   and t.pass_id = p.id  and  p.vocalconten_id = v.id   and  d.id = p.district_id   and o.status =#{status} and DATE_ADD( v.start_time , INTERVAL #{hour} HOUR)  <= now()")
    List<Orders> findPaidOrdersOverStart(Integer hour, Integer status);

    @Select("select create_time from orders")
    List<LocalDateTime> findAllCreateTime();

}
